package dsk.demo.product.controller.product;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import dsk.demo.common.utils.R;
import dsk.demo.product.entity.GuanggaoEntity;
import dsk.demo.product.entity.HotSellEntity;
import dsk.demo.product.entity.ProductEntity;
import dsk.demo.product.service.GuanggaoService;
import dsk.demo.product.service.HotSellService;
import dsk.demo.product.service.ProductService;
import dsk.demo.product.vo.HotSellVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

@RestController
public class HotSellController {

    @Autowired
    private HotSellService hotSellService;

    /**
     * 新增
     */
    @RequestMapping("/hotsell/save")
    public R save(@RequestBody Long[] ids){
        try {
            List<HotSellEntity> entities = new ArrayList<>();
            for (Long i : ids) {
                HotSellEntity entity = new HotSellEntity();
                entity.setProductId(i);
                entities.add(entity);
            }
            hotSellService.saveBatch(entities);
        }catch (Exception e){
            System.out.println(e.getLocalizedMessage());
            return R.error(501,"新增热销产品失败");
        }
        return R.ok();
    }

    /**
     * 查询所有热销
     */
    @RequestMapping("/hotsell/list")
    public R list(){
        try {
            List<HotSellVo> hotSellProduct = hotSellService.getHotSellProduct();
            return R.ok().put("data",hotSellProduct);
        }catch (Exception e){
            return R.error(501,"查询失败");
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/hotsell/delete")
    @ResponseBody
    public R delete(@RequestBody Integer[] ids){
        try {
            hotSellService.removeByIds(Arrays.asList(ids));
        }catch (Exception e){
            return R.error(501,"删除失败");
        }
        return R.ok();
    }

}
